package com.house.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.house.mapper.FangOfficeRentalMapper;
import com.house.mapper.FangRentalMapper;
import com.house.mapper.FangSecondHouseMapper;
import com.house.pojo.FangHouse;
import com.house.pojo.FangOfficeRental;
import com.house.pojo.FangRental;
import com.house.pojo.FangSecondHouse;
import com.house.service.FangHouseService;
import com.house.mapper.FangHouseMapper;
import com.house.utils.R;
import com.house.vo.PageInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lenovo
 * @description 针对表【fang_house(楼盘表)】的数据库操作Service实现
 * @createDate 2025-09-15 11:58:17
 */
@Service
public class FangHouseServiceImpl extends ServiceImpl<FangHouseMapper, FangHouse>
        implements FangHouseService {

    @Autowired
    FangHouseMapper fangHouseMapper;
    @Autowired
    FangOfficeRentalMapper fangOfficeRentalMapper;
    @Autowired
    FangRentalMapper fangRentalMapper;
    @Autowired
    FangSecondHouseMapper fangSecondHouseMapper;

    @Override
    public R houseList(PageInfoVo pageInfoVo) {
        //构造分页参数
        Page<FangHouse> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        //构造查询条件
        LambdaQueryWrapper<FangHouse> wrapper = new LambdaQueryWrapper<>();
        //进行ID倒序排序
        wrapper.orderByDesc(FangHouse::getId);
        //分页查询
        Page<FangHouse> housePage = fangHouseMapper.selectPage(page, wrapper);
        //返回结果
        return R.OK(housePage);
    }

    @Override
    public R officeRentalList(PageInfoVo pageInfoVo) {
        //构造分页参数
        Page<FangOfficeRental> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        //构造查询条件
        LambdaQueryWrapper<FangOfficeRental> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(FangOfficeRental::getId);
        //分页查询
        Page<FangOfficeRental> officeRentalPage = fangOfficeRentalMapper.selectPage(page, wrapper);
        //返回结果
        return R.OK(officeRentalPage);
    }

    @Override
    public R rentalList(PageInfoVo pageInfoVo) {
        //构造分页参数
        Page<FangRental> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        //构造查询条件
        LambdaQueryWrapper<FangRental> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(FangRental::getId);
        //分页查询
        Page<FangRental> rentalPage = fangRentalMapper.selectPage(page, wrapper);
        //返回结果
        return R.OK(rentalPage);
    }

    @Override
    public R secondHouseList(PageInfoVo pageInfoVo) {
        //构造分页参数
        Page<FangSecondHouse> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        //构造查询条件
        LambdaQueryWrapper<FangSecondHouse> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(FangSecondHouse::getId);
        //分页查询
        Page<FangSecondHouse> secondHousePage = fangSecondHouseMapper.selectPage(page, wrapper);
        //返回结果
        return R.OK(secondHousePage);
    }

    @Override
    public R houseDetail(Integer id) {
        //根据ID查询楼盘详情
        FangHouse fangHouse = fangHouseMapper.selectById(id);
        if (fangHouse==null){
            return R.ERROR(500,"楼盘不存在");
        }
        return R.OK(fangHouse);
    }

    @Override
    public R officeDetail(Integer id) {
        //根据ID查询楼盘详情
        FangOfficeRental fangOfficeRental = fangOfficeRentalMapper.selectById(id);
        if (fangOfficeRental==null){
            return R.ERROR(500,"写字楼不存在");
        }
        return R.OK(fangOfficeRental);
    }

    @Override
    public R rentalDetail(Integer id) {
        //根据ID查询楼盘详情
        FangRental fangRental = fangRentalMapper.selectById(id);
        if (fangRental==null){
            return R.ERROR(500,"出租房不存在");
        }
        return R.OK(fangRental);
    }

    @Override
    public R rentalSecondDetail(Integer id) {
        //根据ID查询楼盘详情
        FangSecondHouse fangSecondHouse = fangSecondHouseMapper.selectById(id);
        if (fangSecondHouse==null){
            return R.ERROR(500,"二手房不存在");
        }
        return R.OK(fangSecondHouse);
    }
}




